//admin feedback
$(function(){
	feedback_index.run();
});

var feedback_index = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val() || '';
			this.status = $("#status").val();
			this.feedType = $("#feedType").val();
		},
		
		_start_: function(){
			this._list_feedbacks();
		},
		
		_init_events: function(){
			$("#status").change(function(){
				feedback_index.status = $(this).val();
				feedback_index._list_feedbacks();
			});
			
			$("#feedType").change(function(){
				feedback_index.feedType = $(this).val();
				feedback_index._list_feedbacks();
			});
		},
		
		_list_feedbacks: function(url){
			url = url || this.contextPath + "/admin/feedback/list";
			data = "status=" + this.status + "&feedType=" + this.feedType;
			$.get(url, data, function(result){
				$("#feedback_list_div").html(result);
				feedback_index._bind_fb_events();
			});
		},
		
		_bind_fb_events: function(){
			$("#feedback_list_div > div.pageing > a").click(function(){
				var href = $(this).attr("href");
				feedback_index._list_feedbacks(href);
				return false;
			});
			
			$("#feedback_list_div > div.feedback-block > div.feedback-block-info > a.reply").click(function(){
				var $this = $(this);
				var fid = $this.attr("fid");
				//var $reply = $this.parent().siblings("div.fb-reply");
				var $reply = $(this).parent().parent().find(".addReplyDiv");
				var $h = $reply.html($("#add_reply_div").html());
				$("form > input[name='feedbackId']", $h).val(fid);						
				$("form > :button.add_reply_btn", $h).click(function(){
					var data = $("form", $h).serialize();
					var url = feedback_index.contextPath + "/admin/feedback/reply";
					$.post(url, data, function(result){
						if(result.indexOf('Done') == 0){
							$h.before("Done").html("");
						}else{
							alert(result);
						}
					});
				});
				$("form > :button.cancle", $h).click(function(){
					$reply.html("");
				});
			});
			
			$("#feedback_list_div > div.feedback-block > div.feedback-block-info > a.delete").click(function(){
				var $this = $(this);
				var fid = $this.attr("fid");				
				var url = feedback_index.contextPath + "/admin/feedback/delete/" + fid;				
				$.post(url, function(result){
					if(result.indexOf('Done') == 0){
						location.reload(true);
					}else{
						alert(result);
					}
				});
			});
			
			$("#feedback_list_div > div.feedback-block > div.feedback-block-info > a.undelete").click(function(){
				var $this = $(this);
				var fid = $this.attr("fid");				
				var url = feedback_index.contextPath + "/admin/feedback/undelete/" + fid;				
				$.post(url, function(result){
					if(result.indexOf('Done') == 0){
						location.reload(true);
					}else{
						alert(result);
					}
				});
			});
			
			$("#feedback_list_div > div.feedback-block > div.fb-reply > div.reply_name > a.del_reply").click(function(){
				var r = confirm('Delete reply,are You Sure?');
				if(!r){
					return false;
				}
				data = $(this).prev().val();
				var url = feedback_index.contextPath + "/admin/feedback/reply/delete";
				var $h = $(this).parent().parent();
				$.ajax({
					   type: "POST",
					   url: url,
					   data: "id="+data,
					   success: function(msg){
						   $h.remove();
					   }
					});
			});
		}
};